package com.lyw.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lyw.gulimall.product.entity.AttrEntity;
import com.lyw.gulimall.product.service.AttrAttrgroupRelationService;
import com.lyw.gulimall.product.service.AttrService;
import com.lyw.gulimall.product.service.CategoryService;
import com.lyw.gulimall.product.vo.AttrGroupWithAttrsVo;
import com.lyw.gulimall.product.vo.AttrRelationGroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lyw.gulimall.product.entity.AttrGroupEntity;
import com.lyw.gulimall.product.service.AttrGroupService;
import com.lyw.common.utils.R;



/**
 * 属性分组
 *
 * @author liyawei
 * @email liyawei@gmail.com
 * @date 2022-05-31 11:35:50
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;

    // /0/withattr

    /**
     * 获取分类下的所有属性及分组
     * @param catelogId
     * @return
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId) {

        // 1、查询出来当前分类下的所有属性分组
        // 2、查询每个属性分组中的所有属性
        List<AttrGroupWithAttrsVo> vos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data", vos);
    }

    // /attr/relation
    @PostMapping("/attr/relation")
    public R attrRelation(@RequestBody List<AttrRelationGroupVo> vos) {

        relationService.saveBatch(vos);
        return R.ok();
    }

    /**
     *  获取当前分组没有关联的所有属性
     * @param page
     * @param limit
     * @param key
     * @param attrgroupId
     * @return
     */
    // 3/noattr/relation?t=1656294684574&page=1&limit=10&key=
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R noAttrRelation(@RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
                            @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit,
                            @RequestParam(value = "key", required = false) String key,
                            @PathVariable("attrgroupId") Long attrgroupId) {
        IPage<AttrEntity> list = attrGroupService.getAttrNoRelation(page, limit, attrgroupId, key);
        return R.ok().put("data", list);
    }

    /**
     * 删除属性分组中的关联信息
     * @param vos
     * @return
     */
    // /product/attrgroup/attr/relation/delete
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrRelationGroupVo[] vos) {
        attrGroupService.deleteRelation(vos);

        return R.ok();
    }

    /**
     * 获取属性分组中关联信息
     * @param attrgroupId
     * @return
     */
    // /product/attrgroup/{attrgroupId}/attr/relation
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> list = attrService.getRelationAttr(attrgroupId);

        return R.ok().put("data", list);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    // @RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
                  @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit,
                  @RequestParam(value = "key", required = false) String key,
                  @PathVariable("catelogId") Long catelogId){
        IPage<Map<String, Object>> list = attrGroupService.queryPage(page, limit, catelogId, key);
        return R.ok().put("page", list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    // @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        Long catelogId = attrGroup.getCatelogId();
        Long[] path = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
